<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="82px"
       @submit.native.prevent
    >
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="20">
      <el-col :span="5" style="padding-right:0 !important">
        <left-tree ref="leftRef" @getParentId="getParentIdFunc" @cateChange="cateChange"/>
      </el-col>
      <el-col :span="19" style="padding-left:10px !important">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['base:supplier:add']"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['base:supplier:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['base:supplier:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-download"
              size="mini"
              :loading="btnLoading"
              @click="handleExport"
              v-hasPermi="['base:supplier:export']"
              >导出</el-button
            >
          </el-col>
        </el-row>

        <el-table
        :cell-style="{padding:this.tablePadding}"
          v-loading="loading"
          :data="paramList"
          @selection-change="handleSelectionChange"
          border
        >
          <el-table-column type="selection" width="55" align="center" />
           <el-table-column
            label="供应商名称"
            align="center"
            prop="supplierName"
            :show-overflow-tooltip="true"
          />
           <el-table-column
            label="简称"
            align="center"
            prop="supplierShortName"
          />
          <el-table-column
            label="供应商分类"
            align="center"
            prop="supplierCategoryName"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="地址" align="center" prop="address" :show-overflow-tooltip="true"/>
          <!-- <el-table-column label="税号" align="center" prop="taxno" />
          <el-table-column label="业务范围" align="center" prop="bussiness" :show-overflow-tooltip="true"/> -->
          <el-table-column label="联系人" align="center" prop="contactsName" />
          <el-table-column
            label="联系方式"
            align="center"
            prop="contactsPhone"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="备注" align="center" prop="remark" />
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            width="120"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['base:param:edit']"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['base:param:remove']"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

        <!-- 添加或修改参数定义对话框 -->
        <el-dialog :close-on-click-modal="false"
          :title="title"
          :visible.sync="open"
          width="730px"
          append-to-body
        >
          <el-form ref="form" :inline="true" :model="form" :rules="rules" label-width="120px">
            <el-form-item label="供应商分类" prop="supplierCategoryId">
              <div style="width: 540px">
              <treeselect
                v-model="form.supplierCategoryId"
                :options="categoryList"
                :normalizer="normalizer"
                :show-count="true"
                placeholder="选择供应商分类"
              />
              </div>
            </el-form-item>
            <el-form-item label="供应商名称" prop="supplierName">
            <el-input
                v-model="form.supplierName"
                placeholder="请输入供应商名称"
              />
            </el-form-item>
            <el-form-item label="简称">
              <el-input
                v-model="form.supplierShortName"
                placeholder="请输入简称"
              />
            </el-form-item>
            <el-form-item label="地址">
              <el-input v-model="form.address" placeholder="请输入地址" />
            </el-form-item>
            <el-form-item label="税号">
              <el-input v-model="form.taxno" placeholder="请输入税号" />
            </el-form-item>
            <el-form-item label="业务范围" prop="bussiness">
              <el-input v-model="form.bussiness" placeholder="请输入业务范围" />
            </el-form-item>
            <el-form-item label="联系人" prop="contactsName">
              <el-input
                v-model="form.contactsName"
                placeholder="请输入联系人"
              />
            </el-form-item>
            <el-form-item label="联系方式" prop="contactsPhone">
              <el-input
                v-model="form.contactsPhone"
                placeholder="请输入联系方式"
              />
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="btnLoading" v-funThrottle="[submitForm]">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {
  listSupplier,
  getSupplier,
  delSupplier,
  addSupplier,
  updateSupplier,
  listCategory,
} from "@/api/base/supplier";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import LeftTree from './components/Left.vue'
export default {
  name: "Param",
  components: { LeftTree, Treeselect },
  data() {
    var testPhone = (rule, value, callback) => {
      var isPhone = /^1[3|4|5|6|7|8|9][0-9]\d{8}$/;
      var isMob =/^(\d{3,4}-)?\d{7,8}$/
      if (isMob.test(value) || isPhone.test(value)) {
        return callback();
      } else if(!value){
         return callback();
      }else{
        return callback(new Error('格式错误，如座机请使用"-"连接'));
      }
    };
    return {
      // 遮罩层
      loading: true,
      btnLoading:false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数定义表格数据
      paramList: [],
      dataTypeOptions: [], //数据类型字典
      unitOption: [],
      categoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierName: null,
        supplierCategoryId: null,
      },
      defaultProps: {
        children: "children",
        label: "supplierCategoryName",
      },

      // 表单参数
      form: {},
      selectNames:[],
      // 表单校验
      rules: {
        supplierCategoryId: [
          {
            required: true,
            message: "供应商分类不能为空",
            trigger: "blur",
          },
        ],
        supplierName: [
          { required: true, message: "供应商名称不能为空", trigger: "blur" },
        ],
        contactsPhone: [{ trigger: "blur", validator: testPhone }]
      },
    };
  },
  created() {
  },
  methods: {
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.supplierCategoryId,
        label: node.supplierCategoryName,
        children: node.children,
      };
    },
    cateChange(){
      this.getCategoryList()
    },
    getCategoryList() {
      listCategory().then((response) => {
        this.categoryList = response.data;
      });
    },
    getParentIdFunc(len, id) {
      if (len > 0) {
        this.queryParams.supplierCategoryId = id;
        this.handleQuery();
      } else {
        this.loading = false;
      }
    },
    /** 查询参数定义列表 */
    getList() {
      this.loading = true;
      listSupplier(this.queryParams).then((response) => {
        this.paramList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        paramId: null,
        supplierCategoryId: null,

        dataTypeId: null,
        unitId: null,
        paramCode: null,
        supplierName: null,
        sort: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.supplierId);
      this.selectNames = selection.map((item) => item.supplierName);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.supplierCategoryId=this.queryParams.supplierCategoryId
      this.open = true;
      this.title = "添加供应商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const paramId = row.supplierId || this.ids;
      getSupplier(paramId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改供应商";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.btnLoading=true
          if (this.form.supplierId != null) {
            updateSupplier(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            this.btnLoading=false
            }).catch(()=>{
              this.btnLoading=false
            });
          } else {
            addSupplier(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              // this.getList();
            this.btnLoading=false
             this.$refs.leftRef.setNodeId(this.form.supplierCategoryId);
            }).catch(()=>{
              this.btnLoading=false
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const paramIds = row.supplierId || this.ids;
      const name=row.supplierName||this.selectNames
      this.$confirm(
        '是否确认删除供应商名称为"' + name + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",closeOnClickModal:false,
          type: "warning",
        }
      )
        .then(function () {
          return delSupplier(paramIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.btnLoading=true
      this.download(
        "base/supplier/export",
        {
          ...this.queryParams,
        },
        `base_supplier.xlsx`,
        '',
        ()=>{
          this.btnLoading=false
        }
      );
    },
  },
};
</script>
